package com.smy.controller;

import com.smy.pojo.BasicData;
import com.smy.service.IBasicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/basic")
public class BasicController {
    @Autowired
    private IBasicService service;

    @RequestMapping("/query")
    public String query(BasicData basicData, Model model) throws Exception {
        List<BasicData> list = service.query(basicData);
        model.addAttribute("list", list);
        return "basic/basic";
    }
@RequestMapping("/baseDispatch")
    public String baseDispatch(Integer id,Model model) throws Exception{
    if(id != null){
        // 是更新操作 更加编号查询出对应的基础数据
        BasicData basicData = service.queryById(id);
        model.addAttribute("basicData",basicData);
    }
    List<BasicData> parents = service.queryAllParentData();
    model.addAttribute("parents",parents);
    return "basic/updateBasic";
}
@RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(BasicData data,Model model) throws  Exception{
    if(data.getBaseId() != null && data.getBaseId() > 0){
        // 说明是更新操作
        service.updateBasicData(data);
    }else{
        service.addBasicData(data);
    }

    return "redirect:/basic/query";
}
}
